<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
/**
 *
 *
 * @icon fa fa-circle-o
 */
class Hkj extends Backend
{

    /**
     * HouseType模型对象
     * @var \app\admin\model\HouseType
     */
    protected $model = null;
    protected $searchFields=['name'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\cyclopedia\Disease();

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $hos_id = $this->request->param('hos_id/d');
        $off_id = $this->request->param('off_id/d');
        $hyj =db('hkj')->where(['off_id'=>$off_id,'hos_id'=>$hos_id])->column('d_id');
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            $this->assignconfig('hyj',$hyj);
            return json($result);
        }
        $this->assignconfig('hos_id',$hos_id);
        $this->assignconfig('hyj',$hyj);
        $this->assignconfig('off_id',$off_id);
        return $this->view->fetch();
    }

    public function set2()
    {
        $ids = $this->request->param('ids/a');
        $hos_id = $this->request->param('hos_id/d','');
        $off_id = $this->request->param('off_id/d','');
        if(!$ids){
            $this->error('没有执行任何操作');
        }
        $info = [];
        $haved_industry_ids = db('hkj')->where(['hos_id'=>$hos_id,'off_id'=>$off_id])->column('d_id');
        $haved_industry_ids=$haved_industry_ids?$haved_industry_ids:[];
        $delete_ids = array_diff($haved_industry_ids, $ids);
        $new_ids = array_diff($ids, $haved_industry_ids);
        if(!empty($delete_ids)){   // 删除的
            $del_where['hos_id']= ['eq',$hos_id];
            $del_where['off_id']= ['eq',$off_id];
            $del_where['d_id']= ['in',$delete_ids];
            db('hkj')->where($del_where)->delete();
        }
        if(!empty($new_ids)){  // 新增的
            $add_data=[];
            foreach ($new_ids as $nv){
                $add_data[]=array('hos_id'=>$hos_id,'d_id'=>$nv,'off_id'=>$off_id,'create_time'=>time());
            }
            if(!empty($add_data)){
                db('hkj')->insertAll($add_data);
            }
        }
//        $res = $this->model->isUpdate()->saveAll($info);//更新数据
//        if($res){
//            $this->success('操作成功');
//        }
        $this->success('操作成功');

    }
    public function edit($ids=null)
    {
        $hos_id = $this->request->param('hos_id/d','');
        $off_id = $this->request->param('off_id/d','');
        $rows=  db('office')->where('id',$off_id)->find();
        $hospital=  db('hospital')->where('id',$hos_id)->find();
        $des=  db('hkj')->where(['hos_id'=>$hos_id,'off_id'=>$off_id])->column('d_id');
        $des= implode(',',$des);
        $this->assign('hos_id',$hos_id);
        $this->assign('off_id',$off_id);
        $this->assign('row',$rows);
        $this->assign('hospital',$hospital);
        $this->assign('des',$des);
        return $this->view->fetch();
    }
    public function set()
    {

        $params = $this->request->post('row/a');
        $hos_id = $params['hos_id'];
        $off_id = $params['off_id'];
        $ids = $params['disease_id'];
        if(!$ids){
            $this->error('没有执行任何操作');
        }
        $ids = explode(',',$ids);
        $info = [];
        $haved_industry_ids = db('hkj')->where(['hos_id'=>$hos_id,'off_id'=>$off_id])->column('d_id');
        $haved_industry_ids=$haved_industry_ids?$haved_industry_ids:[];
        $delete_ids = array_diff($haved_industry_ids, $ids);
        $new_ids = array_diff($ids, $haved_industry_ids);
        if(!empty($delete_ids)){   // 删除的
            $del_where['hos_id']= ['eq',$hos_id];
            $del_where['off_id']= ['eq',$off_id];
            $del_where['d_id']= ['in',$delete_ids];
            db('hkj')->where($del_where)->delete();
        }
        if(!empty($new_ids)){  // 新增的
            $add_data=[];
            foreach ($new_ids as $nv){
                $add_data[]=array('hos_id'=>$hos_id,'d_id'=>$nv,'off_id'=>$off_id,'create_time'=>time());
            }
            if(!empty($add_data)){
                db('hkj')->insertAll($add_data);
            }
        }
//        $res = $this->model->isUpdate()->saveAll($info);//更新数据
//        if($res){
//            $this->success('操作成功');
//        }
        $this->success('操作成功');

    }
}
